/**
 对象流的两个类
 ObjectOutputStream 将java对象的基本数据类型和图形写入OutputStream
 ObjectInputStream对以前使用ObjectOutputStream写入的基本数据和对象进行反序列化
 
 序列化一组对象：
 在序列化操作中，同时序列化多个对象时，反序列化也必须按顺序操作，如果想要序列化一组对象该如何操作呢
 序列化一组对象可采用：对象数组的形式，因为对象数组可以向Object进行转型操作
 
 transient关键字
 如果用transient声明一个实例变量，当对象储存时，它的值不需要维持。
 
 重点 序列化传输只看动态的常量(保存对象的状态)，静态变量不关注
 */

/*
package com.leo.第八章_文件与IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.InputStream;
public class U8_L9_对象流 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		run();

	}
	private static void run() {
		File f=new File("E:\\eclipse-workspace\\test\\RW\\objio.obj");
		if(!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Obj_Write(f);
		Obj_Read(f);
	}
	
	//序列化就是用于打包 和 传输
	private static void Obj_Write(File f) {
		try {
			Dog dog1=new Dog("小黄",2,"公");
			Dog dog2=new Dog("小红",3,"母");
			Dog[]dogs= {dog1,dog2};
			OutputStream out=new FileOutputStream(f);
			ObjectOutputStream oos=new ObjectOutputStream(out);
			oos.writeObject(dogs);
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("写入完毕  " +f.getAbsolutePath());
	}
	//反序列化就是用于展开和运行对象
	private static void Obj_Read(File f) {
		try {
			InputStream in=new FileInputStream(f);
			ObjectInputStream ois=new ObjectInputStream(in);
			Dog[] dogs=(Dog[])ois.readObject();
			ois.close();
			for(Dog dog:dogs) {
				System.out.println(dog.toString());
			}
			System.out.println("读取完毕!  "+f.getAbsolutePath());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}	
}

//记得加 接口Serializable  不然会报错 Serializable 的错误
class Dog implements Serializable{	
	//这句是让读写一致 确保读写是同一个UID
	private static final long serialVersionUID = 1L;
	private String name;
	private String sex;
	private int age;
	//这个关键字就是不在后续的读取中出现，只在写入前留个记录
	private transient int id;
	public Dog() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Dog(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Dog [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
}
*/